package cn.wlxctech.controller;

import cn.wlxctech.common.utils.MongoDbUtil;
import cn.wlxctech.entity.Tuser;
import cn.wlxctech.fegin.ArticleFein;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

/**
 * @author WanShen
 * @date 2024年04月23日 21:33
 */
@RestController
@RequestMapping("/user")
public class TestController {

    @Autowired
    private ArticleFein articleFein;

    @Autowired
    private MongoDbUtil mongoDbUtil;

    @GetMapping("/demo")
    public String demo() {
        return "user";
    }

    @GetMapping("/articleId")
    public String articleId(long id) {
        return articleFein.articleId(id);
    }

    @GetMapping("/addUser")
    public String addUser(String userName) {
        Tuser tuser = new Tuser();
        tuser.setUserName(userName);
        tuser.setPassword("123456");
        tuser.setId(new Random().nextLong());
        MongoDbUtil.save(tuser);
        return "addUser-ok!";
    }

    @GetMapping("/getUserByUserName")
    public List<?> getUserArticle(String userName) {
        if (null==userName || userName.isEmpty() ){
            return MongoDbUtil.findAll(new Tuser());
        }
        List<?> objects = MongoDbUtil.find(new Tuser(), new String[]{"userName"}, new Object[]{userName});

        return objects;
    }

    @GetMapping("/getAllUser")
    public List<?> getAllUsers(){
        return MongoDbUtil.findAll(new Tuser());
    }

}
